package com.hexiang.spring.dao;

import com.hexiang.dao.OmsOrderItemDao;
import com.hexiang.domain.OmsOrderItem;
import com.hexiang.exception.MyDaoException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

@Repository
@Slf4j
public class JdbcTemplateOmsOrderItemDaoImpl implements OmsOrderItemDao {

    @Autowired
    private DataSource dataSource;

    private JdbcTemplate jdbcTemplate;

    @PostConstruct
    private void postConstruct() {
        jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Override
    public Long save(OmsOrderItem omsOrderItem) {
        KeyHolder keyHolder = new GeneratedKeyHolder();

        String sql = "INSERT INTO t_oms_order_item (order_id, order_sn, product_id, product_pic, product_name, product_brand, product_sn, product_price, product_quantity, product_sku_id, product_sku_code, product_category_id) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";

        jdbcTemplate.update(conn -> {
            PreparedStatement ps = null;
            try {
                ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                ps.setLong(1, omsOrderItem.getOrderId());
                ps.setString(2, omsOrderItem.getOrderSn());
                ps.setLong(3, omsOrderItem.getProductId());
                ps.setString(4, omsOrderItem.getProductPic());
                ps.setString(5, omsOrderItem.getProductName());
                ps.setString(6, omsOrderItem.getProductBrand());
                ps.setString(7, omsOrderItem.getProductSn());
                ps.setBigDecimal(8, omsOrderItem.getProductPrice());
                ps.setInt(9, omsOrderItem.getProductQuantity());
                ps.setLong(10, omsOrderItem.getProductSkuId());
                ps.setString(11, omsOrderItem.getProductSkuCode());
                ps.setLong(12, omsOrderItem.getProductCategoryId());
            } catch (SQLException e) {
                log.error("saveOmsOrderItemErr", e);
                throw new MyDaoException(e);
            }
            return ps;
        }, keyHolder);
        return keyHolder.getKey().longValue();
    }

    @Override
    public OmsOrderItem get(long id) {
        return null;
    }

    @Override
    public Integer deleteById(long id) {
        return null;
    }

    @Override
    public Integer update(OmsOrderItem omsOrderItem) {
        return null;
    }

    @Override
    public List<OmsOrderItem> queryAll() {
        return null;
    }

    @Override
    public List<OmsOrderItem> queryListByLimit(int limit) {
        return null;
    }

    @Override
    public Integer count() {
        return null;
    }

    @Override
    public int[][] batchSave(List<OmsOrderItem> list) {
        return new int[0][];
    }

    @Override
    public int[][] batchUpdate(List<OmsOrderItem> tist) {
        return new int[0][];
    }
}